#include "string.h"
#include "protect.h"
#include "global.h"
#include "sub.h"

void cstart() {
    memcpy(&g_gdt, g_gdt_ptr.base, g_gdt_ptr.limit + 1);
    g_gdt_ptr.base = (unsigned char*)&g_gdt;
    g_gdt_ptr.limit = sizeof(g_gdt) - 1;

    g_idt_ptr.base = (unsigned char*)&g_idt;
    g_idt_ptr.limit = sizeof(g_idt) - 1;
    init_prot();
}

void exception_handler(int vec_no,int err_code,int eip,int cs,int eflags) {
    reset_sec_reg();
    disable_irq(0); //clock-interrupt
    disable_irq(1); //key-interrupt
    set_text_attr(TA_FOREGROUND_RED | TA_FOREGROUND_INTESITY);

    char * err_msg[] = {"#DE Divide Error",
                "#DB RESERVED",
                "--  NMI Interrupt",
                "#BP Breakpoint",
                "#OF Overflow",
                "#BR BOUND Range Exceeded",
                "#UD Invalid Opcode (Undefined Opcode)",
                "#NM Device Not Available (No Math Coprocessor)",
                "#DF Double Fault",
                "    Coprocessor Segment Overrun (reserved)",
                "#TS Invalid TSS",
                "#NP Segment Not Present",
                "#SS Stack-Segment Fault",
                "#GP General Protection",
                "#PF Page Fault",
                "--  (Intel reserved. Do not use.)",
                "#MF x87 FPU Floating-Point Error (Math Fault)",
                "#AC Alignment Check",
                "#MC Machine Check",
                "#XF SIMD Floating-Point Exception"
    };

    printf("Exception! --> %s, \n", err_msg[vec_no]);
    printf("EFLAGS:0x%X, ", eflags);
    printf("CS:0x%X, ", cs);
    printf("EIP:0x%X\n", eip);

    if(err_code != 0xFFFFFFFF){
        printf("Error code:0x%X\n", err_code);
    }
    set_text_attr(TA_TEXT_WHITE);
}


